var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
var curX = 0;
var curY = 0;
var post;
var hitNum = 0;
var animFirst = false;

var fly;
//to line up in the end, incWidth must result in an integer
var targetWidth = 305;
var startWidth = 260;
var targetHeight = 227;
var startHeight = 91;
var slideSteps = 10;
var incWidth = (targetWidth - startWidth) / slideSteps;
var incHeight = (targetHeight - startHeight) / slideSteps;

/* Storyboard Functions */
		
function selectPost(num){
	selectPost(num, false);
}
	
function selectPost(num, initRun){
	animFirst = initRun;	
	reorgSmall(num);
	hitNum = num;
	if(animFirst)
		hideAllBig();
}			
		
function hideAllBig(){
	var x = document.getElementsByTagName('div');
	var cnt = 0;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].className == 'post')
		{
			cnt++;
			if(cnt == hitNum)
				x[i].style.display = 'block';
			else
				x[i].style.display = 'none';
		}		
	}
}
		
function reorgSmall(num){
	var x = document.getElementsByTagName('div');
	var cnt = 0;
							
	for (var i=0;i<x.length;i++)
	{
		if (x[i].className == 'smallPost')
		{
			//alert(x[i].id);
			if(x[i].id == ('smallPost' + num))
			{
				x[i].style.backgroundImage = "url(" + animPath + "smallPost_bgd.gif)";
				x[i].style.backgroundColor = "#fbe383";
				x[i].style.backgroundRepeat = "repeat-x";
				
				moveBox(findPosX(x[i]), findPosY(x[i]), num);
			}						
			else
			{
				x[i].style.backgroundImage = "url(" + animPath + "a.gif)";
				x[i].style.backgroundColor = "#efefe8";
			}
			cnt++;					
		}
	}
}			

function moveBox(x, y, num)
{
	var wwwh = document.getElementById("center");

	anim1 = new animation("fly1");	
	anim1.moveTo(x, y);
	anim1.setWidth(startWidth);
	anim1.setHeight(startHeight);
	if(!animFirst)
		anim1.slideTo(findPosX(wwwh) + 15, findPosY(wwwh) + 40, slideSteps, 25, "hideAllBig();");
	else
		anim1.setOpacity(0);
}

/*  Animation functions */

function animation(id) {

  this.element = (NS4) ? document[id] : document.all[id].style;
  this.active = 0;
  this.timer = null;
  this.path = null;
  this.num = null;

  this.name = id + "Var";  
  eval(this.name + " = this");
  this.actName = id;

  this.animate = animate;
  this.step = step;
  this.show = show;
  this.hide = hide;
  this.left = left;
  this.top = top;
  this.moveTo = moveTo;
  this.slideBy = slideBy;
  this.slideTo = slideTo;
  this.circle = circle;
  this.setOpacity = setOpacity;
  this.setWidth = setWidth;
  this.setHeight = setHeight;
  
  fly = document.getElementById(this.actName);
}

function pos(x, y) {
  this.x = Math.round(x);
  this.y = Math.round(y);
}

function show() {
  this.element.visibility = (NS4) ? "show" : "visible";
}

function hide() {
  this.element.visibility = (NS4) ? "hide" : "hidden";
}

function left() {
  return parseInt(this.element.left);
}

function top() {
  return parseInt(this.element.top);
}

function addWidth(amt)
{
	fly.style.width = fly.offsetWidth + amt;
}
function setWidth(amt)
{
	fly.style.width = amt;
}
function addHeight(amt)
{
	fly.style.height = fly.offsetHeight + amt;
}
function setHeight(amt)
{
	fly.style.height = amt;
}

function setOpacity(num){
	
	num = Math.abs(100-num);
	
	if(num == 10)
		num = 0;
	
	browserdetect=fly.filters? "ie" : typeof fly.style.MozOpacity=="string"? "mozilla" : ""
	if(num == 0){
		fly.style.left = -500;
	}
	else{
		if (browserdetect=="mozilla")
			fly.style.MozOpacity=num/100;
		else if (browserdetect=="ie")
			fly.filters.alpha.opacity=num;
		else
			fly.style.opacity = num/100;
	}	
}

function moveTo(x, y) {
  this.element.left = x;
  this.element.top = y;
}

function step() {
	this.moveTo(this.path[this.num].x, this.path[this.num].y);
	this.setOpacity(Math.abs((this.num/this.path.length)*100));
	addWidth(incWidth);
	addHeight(incHeight);
	if (this.num >= this.path.length - 1) {
		clearInterval(this.timer);
		this.active = 0;
		if (this.statement)
			eval(this.statement);
    }
	else
	{
		this.num++;
	}
}

function animate(interval) {
  if (this.active) return;
  this.num = 0;
  this.active = 1;
  //NEED TO SHUT THIS OFF DUE TO BUG WITH PLAYER CONTROLS AFTER ANIMATION SEQUENCE...
  this.timer = setInterval(this.name + ".step()", interval);
}

function slideBy(dx, dy, steps, interval, statement) {
  var fx = this.left();
  var fy = this.top();
  var tx = fx + dx;
  var ty = fy + dy; 
  
  this.slideTo(tx, ty, steps, interval, statement);
  
}

function slideTo(tx, ty, steps, interval, statement) {
  var fx = this.left();
  var fy = this.top();
  var dx = tx - fx;
  var dy = ty - fy;
  var sx = dx / steps;
  var sy = dy / steps;

  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    fx += sx;
    fy += sy;
    ar[i] = new pos(fx, fy);
  }
    
  this.path = ar;  
  this.statement = (statement) ? statement : null;
  this.animate(interval);
}

function circle(radius, angle0, angle1, steps, interval, statement) {
  var dangle = angle1 - angle0;
  var sangle = dangle / steps;
  var x = this.left();
  var y = this.top();
  var cx = x - radius * Math.cos(angle0 * Math.PI / 180);
  var cy = y + radius * Math.sin(angle0 * Math.PI / 180);

  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    angle0 += sangle;
    x = cx + radius * Math.cos(angle0 * Math.PI / 180);
    y = cy - radius * Math.sin(angle0 * Math.PI / 180);
    ar[i] = new pos(x, y);
  }
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}